package com.hpx.javaeye.util;

import net.rim.device.api.ui.component.Dialog;

public class JsonUtil {
	private static BlackberryLogger logger = BlackberryLogger.getInstance();

	public static String[][] extractFromJson(String target) {
		String[][] dict = new String[2][];
		if (target == null && "".equals(target))
			return dict;

		// remove braces
		int len = target.length();
		target = target.substring(1, len - 1);
		System.out
				.println("[debug huangpengxiao] after remove braces target string is :"
						+ target);
		// split with comma,colon,quotes
		int cnt = 0;
		int idx = 0;
		logger.log("target.indexOf(,) is : " + target.indexOf(","));

		while ((idx = target.indexOf(",", idx + 1)) != -1) {
			cnt++;
		}

		for (int i = 0; i < cnt; i++) {
			dict[0] = new String[cnt];
			dict[1] = new String[cnt];
		}
		idx = 0;
		int startPos = 0;
		int ridx = 0;
		while ((idx = target.indexOf(",", startPos)) != -1) {
			String seg = target.substring(startPos, idx);
			logger.log("[debug huangpengxiao] seg is : " + seg);
			logger.log("[debug huangpengxiao] idx is : " + idx);
			seg = seg.trim();
			int pos = 0;
			if ((pos = seg.indexOf(":")) != -1) {
				logger.log("[debug huangpengxiao] pos is : " + pos);

				dict[0][ridx] = new String(seg.substring(1, pos - 1));
				logger.log("[debug huangpengxiao] dict[0][ridx] : "
						+ dict[0][ridx]);
				logger.log("[debug huangpengxiao] seg.charAt(seg.length() - 1 : "
								+ seg.charAt(seg.length() - 1));
				if (seg.charAt(seg.length() - 1) == '"') {
					dict[1][ridx] = new String(seg.substring(pos + 3, seg.length() - 1));
				} else {
					dict[1][ridx] = new String(seg.substring(pos + 2, seg.length()));
				}

				logger.log("[debug huangpengxiao] dict[1][ridx] : "
						+ dict[1][ridx]);

			}
			ridx++;
			startPos = idx + 1;
		}

		return dict;
	}

}
